//
//  MGChannelManageExampleVC.swift
//  weibo
//
//  Created by apple on 2023/7/7.
//  Copyright © 2023 陈明光. All rights reserved.
//

import UIKit

class MGChannelManageExampleVC: UIViewController {

    var pageViewController:XLPageViewController!
    //使用中的标题
    var enableTitles = ["要闻","财经","娱乐","体育","社会","NBA","视频","汽车","图片","科技","军事","国际","数码","星座","电影","时尚","文化","游戏"]
    //未使用的标题
    var disableTitles = ["有声","家居","电竞","美容","电视剧","搏击","健康","摄影","生活","旅游","韩流"]
    override func viewDidLoad() {
        super.viewDidLoad()
        initPageViewController()
    }
    

    func initPageViewController(){
        let config = XLPageViewControllerConfig.default()
        config.titleSelectedColor = UIColor.cz_color(withHex: 0xFF8C00)
        config.titleNormalColor = .black
        config.shadowLineHidden = true
        config.titleSpace = 20
        config.titleSelectedFont = UIFont.systemFont(ofSize: 17)
        config.titleNormalFont = UIFont.systemFont(ofSize: 17)
        config.titleColorTransition = false
        
        pageViewController = XLPageViewController.init(config: config)
        pageViewController.view.frame = CGRectMake(0, APP_STATUS_BAR_HEIGHT + 44.0, APP_SCREEN_WIDTH, APP_SCREEN_HEIGHT - (APP_STATUS_BAR_HEIGHT + 44.0))
        
        pageViewController.delegate = self
        pageViewController.dataSource = self
        let channelManageButton = UIButton()
        channelManageButton.addTarget(self, action:  #selector(channelManage), for: .touchUpInside)
        channelManageButton.setImage(UIImage(named: "channelManageButton"), for: .normal)
        channelManageButton.imageEdgeInsets = UIEdgeInsets(top: 5, left: 7, bottom: 5, right: 7)
        pageViewController.rightButton = channelManageButton
        self.addChild(pageViewController)
        self.view.addSubview(pageViewController.view)
    }
    
    
    @objc func channelManage(){
        XLChannelControl.share().showChannelView(withEnabledTitles: self.enableTitles, disabledTitles: disableTitles) { enabledTitles, disabledTitles in

            self.enableTitles = enabledTitles as? [String] ?? [String]()
            self.disableTitles = disabledTitles as? [String] ?? [String]()
            self.pageViewController.reloadData()
            self.pageViewController.selectedIndex = 0
        }
    }
}

extension MGChannelManageExampleVC:XLPageViewControllerDelegate,XLPageViewControllerDataSrouce{
    func pageViewController(_ pageViewController: XLPageViewController, didSelectedAt index: Int) {
        print("切换到\(enableTitles[index])")
    }
    
    func pageViewController(_ pageViewController: XLPageViewController, viewControllerFor index: Int) -> UIViewController {
        let vc = MGDemoViewController()
        vc.view.backgroundColor = .red
        return vc
    }
    
    func pageViewController(_ pageViewController: XLPageViewController, titleFor index: Int) -> String {
        enableTitles[index]
    }
    
    func pageViewControllerNumberOfPage() -> Int {
        enableTitles.count
    }
    
    
}
